package org.dromara.tearoom.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.tearoom.domain.DayStat;
import org.dromara.tearoom.domain.HourStat;
import org.dromara.tearoom.domain.TerOrder;
import org.dromara.tearoom.domain.bo.TerOrderBo;
import org.dromara.tearoom.domain.vo.TerOrderDayVo;
import org.dromara.tearoom.domain.vo.TerOrderRoomVo;
import org.dromara.tearoom.domain.vo.TerOrderVo;

import java.util.List;
import java.util.Map;

/**
 * 订单Mapper接口
 *
 * @author xch
 * @date 2025-03-29
 */

public interface TerOrderMapper extends BaseMapperPlus<TerOrder, TerOrderVo> {
    @Select("SELECT o.*, r.teroom_imgurl FROM ter_order o LEFT JOIN ter_room_details r ON o.tearoom_id = r.id WHERE o.open_id = #{openId} and  o.order_status= #{orderStatus}  order by  o.update_time DESC,o.create_time DESC" )
    List<TerOrderRoomVo> selectOrderWithUser(@Param("openId") String openId, @Param("orderStatus") String orderStatus);

    @Select("SELECT  COALESCE(SUM(prepay_amount), 0)  AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4')  AND create_time >= CURDATE() ")
    TerOrderDayVo selectOrderCountTd();

    @Select("SELECT COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice, COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4')  AND create_time > DATE_SUB(CURDATE(), INTERVAL #{daynum} DAY) AND create_time <= NOW()")
    TerOrderDayVo selectOrderCountDn(@Param("daynum") Integer daynum);

    @Select("SELECT  COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4') AND create_time >= #{start} AND create_time < DATE_ADD(#{end}, INTERVAL 1 DAY) ")
    TerOrderDayVo selectOrderCountStarAndEnd(@Param("start") String start , @Param("end") String end);

    @Select("SELECT  COALESCE(SUM(prepay_amount), 0)  AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status ='5' AND create_time >= CURDATE() ")
    TerOrderDayVo selectRefundAmount();

    @Select("SELECT COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice, COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status ='5'  AND create_time > DATE_SUB(CURDATE(), INTERVAL #{daynum} DAY) AND create_time <= NOW()")
    TerOrderDayVo selectRefundAmountDn(@Param("daynum") Integer daynum);

    @Select("SELECT  COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status ='5' AND create_time >= #{start} AND create_time < DATE_ADD(#{end}, INTERVAL 1 DAY) ")
    TerOrderDayVo selectRefundAmountStarAndEnd(@Param("start") String start , @Param("end") String end);
    @Select("SELECT  COALESCE(SUM(prepay_amount), 0)  AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4') AND payment_method =#{payMethod} AND create_time >= CURDATE() ")
    TerOrderDayVo selectOrderBalancePay(@Param("payMethod") String payMethod);

    @Select("SELECT COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice, COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4') AND payment_method =#{payMethod}   AND create_time > DATE_SUB(CURDATE(), INTERVAL #{daynum} DAY) AND create_time <= NOW()")
    TerOrderDayVo selectOrderBalancePayDn(@Param("daynum") Integer daynum,@Param("payMethod") String payMethod);

    @Select("SELECT  COALESCE(SUM(prepay_amount), 0) AS dailyTotalPrice,  COUNT(*) AS dailyOrderCount FROM ter_order WHERE order_status IN ('1', '2', '3', '4')AND payment_method =#{payMethod} AND create_time >= #{start} AND create_time < DATE_ADD(#{end}, INTERVAL 1 DAY) ")
    TerOrderDayVo selectOrderBalancePayStarAndEnd(@Param("start") String start , @Param("end") String end,@Param("payMethod") String payMethod);

    /**
     * 重置订单价格和优惠券
     * @param order
     * @return
     */
    Boolean resetOrderPrice(@Param("order") TerOrderVo order);

    /**
     * 判断时间段是否被占用
     * @param orderBo
     * @return
     */
    Integer isTimeSlotAvailable(@Param("order") TerOrderBo orderBo);

    /**
     * 统计今天的订单数据
     * @param date
     * @return
     */
    @Select("SELECT DATE_FORMAT(create_time, '%H:00') AS time_unit, COUNT(id) AS count " +
            "FROM ter_order " +
            "WHERE DATE(create_time) = #{date} AND order_status IN ('1','2','3')" +
            "GROUP BY HOUR(create_time)")
    List<HourStat> getHourlyStats(@Param("date") String date);

    /**
     * 统计每天的订单数据
     * @param start
     * @param end
     * @return
     */
    @Select("SELECT DATE_FORMAT(create_time, '%Y-%m-%d') AS time_unit, COUNT(id) AS count " +
            "FROM ter_order " +
            "WHERE DATE(create_time) BETWEEN #{start} AND #{end} AND order_status IN ('1','2','3')" +
            "GROUP BY DATE(create_time)")
    List<DayStat> getDailyStats(@Param("start") String start, @Param("end") String end);


    /**
     * 联表查询订单详情（包含房间状态和订阅信息）
     *
     * @param page 分页参数
     * @param bo 查询条件
     * @return 分页结果
     */
    Page<TerOrderVo> selectVoPageWithDetails(@Param("page") Page<TerOrder> page, @Param("bo") TerOrderBo bo);

    /**
     * 联表查询订单详情（包含房间）
     *
     * @param page 分页参数
     * @param bo 查询条件
     * @return 分页结果
     */
    Page<TerOrderVo> selectVoAppPage(@Param("page") Page<TerOrder> page, @Param("bo") TerOrderBo bo);

    /**
     * 查询空间使用的最近一笔订单
     * @param roomId
     * @return
     */
    TerOrderVo selectNearestOrder(@Param("roomId") Long roomId);

    /**
     * 根据时间段查询当前房间是否可以预定
     * @param startTime
     * @param endTime
     * @return
     */
    Integer checkTearoomAvailability(@Param("tearoomId") Long tearoomId, @Param("startTime") String startTime, @Param("endTime") String endTime);


    /**
     * 查询结束时间早于当前时间但不超过多少分钟的订单
     * @param minutes
     * @param openid
     * @return
     */
    Integer selectRecentlyExpiredOrders(@Param("minutes") int minutes,@Param("openid") String openid);

}
